package com.wxj.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxj.edu.entity.Chapter;
import com.wxj.edu.entity.Video;
import com.wxj.edu.entity.chapter.ChapterVo;
import com.wxj.edu.entity.chapter.VideoVo;
import com.wxj.edu.mapper.ChapterMapper;
import com.wxj.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxj.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wxj
 * @since 2021-03-10
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    /**
     * 1 课程大纲列表，根据课程id进行查询
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //根据课程id查询课程里面所有的章节
        QueryWrapper<Chapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<Chapter> chaptersList = baseMapper.selectList(wrapperChapter);

        //查询小节
        QueryWrapper<Video> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<Video> videoList = videoService.list(wrapperVideo);

        //创建list集合用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节list集合进行封装
        for (Chapter chapter : chaptersList) {
            ChapterVo chapterVO = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVO);
            finalList.add(chapterVO);

            List<VideoVo> videoVoList = new ArrayList<>();

            //遍历查询小节list集合进行封装
            for (Video video : videoList) {
                if (video.getChapterId().equals(chapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    //放到小节封装集合
                    videoVoList.add(videoVo);
                }
            }
            chapterVO.setChildren(videoVoList);
        }
        return finalList;
    }

    /**
     * 4 删除章节的方法
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterid章节id 查询小节表，如果查询数据，不进行删除
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
//        int count = videoService.count(wrapper);
        //判断
//        if (count >= 0) {    //查询出小节，不进行删除
        videoService.removeVideoByChapterId(chapterId);
        int result = baseMapper.deleteById(chapterId);
        return result > 0;
//        } else {
//            throw new BaseException(20001,"不能删除");
//        }

    }

    /**
     * 5 根据课程id删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }


}
